﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Portal.Web.Model;
using Portal.Utility;
using Portal.Web.Utility;
using Portal.Exam.Bussiness;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class AddExamTemplate
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AddExamTemplate));
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                string BackUrl = "ExamTemplate.aspx";
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string AccountType = WebEnvironments.Request(SiteId, "AccountType");
                if (string.IsNullOrEmpty(AccountType)) AccountType = "";

                string RetHtml =
                    "<input type=hidden id=txtAccountType value=\"" + AccountType + "\">\r\n" +
                    "<script language=javascript>\r\n" +
                    " function ClientSaveAddExamTemplate()\r\n" +
                    "     {\r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         AccountType=document.getElementById(\"txtAccountType\").value;\r\n" +
                    "         ExamTemplateName=document.getElementById(\"txtExamTemplateName\").value;\r\n" +
                    "         ExamTime=document.getElementById(\"txtExamTime\").value;\r\n" +
                    "         MaxQuestion=\"0\";//document.getElementById(\"txtMaxQuestion\").value;\r\n" +
                    "         PassPoint=document.getElementById(\"txtPassPoint\").value;\r\n" +
                    "         IndexOrder=\"0\";//document.getElementById(\"txtIndexOrder\").value;\r\n" +
                    "         IsFreeText= document.getElementById(\"chkIsFreeText\").checked; \r\n" +
                    "         BeforeExamGuidle=document.getElementById(\"txtBeforeExamGuidle\").value;\r\n" +

                    "         AjaxOut = Portal.Exam.Render.AddExamTemplate.ServerSideAddExamTemplate(SiteId,AccountType, ExamTemplateName,ExamTime,PassPoint,BeforeExamGuidle,IsFreeText).value;\r\n" +

                    "     if(AjaxOut.Error) \r\n" +
                    "     { \r\n" +
                    "         alert(AjaxOut.InfoMessage); \r\n" +
                    "         return; \r\n" +
                    "     } \r\n" +
                    "     window.open(AjaxOut.RetUrl,'_self');\r\n" +

                    "         document.getElementById(\"txtExamTemplateName\").value=\"\";\r\n" +
                    "         document.getElementById(\"txtExamTime\").value=\"0\";\r\n" +
                    "         document.getElementById(\"txtMaxQuestion\").value=\"0\";\r\n" +
                    "         document.getElementById(\"txtPassPoint\").value=\"0\";\r\n" +
                    "         document.getElementById(\"txtIndexOrder\").value=\"0\";\r\n" +
                    "         document.getElementById(\"txtBeforeExamGuidle\").value=\"\";\r\n" +
                    "         document.getElementById(\"txtExamTemplateName\").focus();\r\n" +
                    "     }\r\n" +

                    " function DetectMonthi() {\r\n" +
                    "    SiteId='" + SiteId + "';\r\n" +
                    "    GroupQuestionId = document.getElementById(\"drpSelectGroupQuestion\").options[document.getElementById(\"drpSelectGroupQuestion\").selectedIndex].value;\r\n" +
                    "    retValue = Portal.Exam.Render.AddExamTemplate.ReadingMonthi(SiteId, GroupQuestionId).value;\r\n" +
                    "    if (retValue.indexOf(\"ERROR\") != -1) {\r\n" +
                    "        alert(retValue);\r\n" +
                    "        return;\r\n" +
                    "    }\r\n" +
                    "    document.getElementById(\"divMonthi\").innerHTML = retValue;\r\n" +
                    " }\r\n" +

                    "function ResetSearch()\r\n" +
                    "{\r\n" +

                    "}\r\n" +
                    "</script>\r\n" +

                  " <div class=DatabaseFunction>\r\n" +
                    "        <ul>\r\n" +
                    "            <li onclick=\"javascript:ClientSaveAddExamTemplate();\"><img class=image src=\"" + IconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                    "            <li onclick=\"javascript:window.open('" + BackUrl + "','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                    "        </ul>\r\n" +
                    "  </div>\r\n" +
                    "<h3>Thêm mẫu đề thi</h3>\r\n" +
                "   <div class=CssFormSaveUpdate>\r\n" +
                "     <table style=\"width:99%\">" +
                "     <tr>" +
                "         <td class=\"Col1\">Giới thiệu trước khi thi:</td>" +
                "         <td><textarea id=txtBeforeExamGuidle class=textarea></textarea></td>" +
                "     </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Đề thi:</td>" +
                "              <td><input class=textbox id=txtExamTemplateName></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Thời gian làm bài:</td>" +
                "              <td><input class=textbox id=txtExamTime></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Điểm đạt</td>" +
                "              <td><input class=textbox id=txtPassPoint></td>" +
                "          </tr>" +

                "          <tr>" +
                "              <td class=\"Col1\">Tổng số câu hỏi:</td>" +
                "              <td><input class=textbox id=txtMaxQuestion></td>" +
                "          </tr>" +

                "          <tr>" +
                "              <td class=\"Col1\">Thứ tự:</td>" +
                "              <td><input class=textbox id=txtIndexOrder></td>" +
                "          </tr>" +

                "          <tr>" +
                "              <td class=\"Col1\">Là đề thi tự luận:</td>" +
                "              <td><input type=\"checkbox\" id=\"chkIsFreeText\"></td>" +
                "          </tr>" +
                "      </table>" +
                "</div>\r\n";


                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideAddExamTemplate(
            string SiteId,
            string AccountType,
            string ExamTemplateName,
            string ExamTime,
            string PassPoint,
            string BeforeExamGuidle,
            bool IsFreeText)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();

                if (string.IsNullOrEmpty(ExamTemplateName))
                {
                    throw new Exception("Nhập tiêu đề!");
                }
                if (FunctionUtilities.checkInteger(ExamTime) == false)
                {
                    throw new Exception("Thời gian nhập sai!");
                }
                if (FunctionUtilities.checkInteger(PassPoint) == false)
                {
                    throw new Exception("Điểm đạt nhập sai!");
                }
                string ExamTemplateId = new ExamTemplateBll().CreateExamTemplate(null, SiteId,AccountType, ExamTemplateName, int.Parse(ExamTime), 0, int.Parse(PassPoint), 0, BeforeExamGuidle, IsFreeText);
                string Url = "UpdateExamTemplate.aspx?ExamTemplateId=" + ExamTemplateId;
                OAjaxOut.RetUrl = Url;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.Error = true;
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public string ReadingMonthi(string SiteId, string GroupQuestionId)
        {
            DataTable dtMonthi = new MonthiBll().ReadingMonthi(SiteId, GroupQuestionId, "");
            string SelectMonthiText = "<select class=cssComboboxContent id=drpSelectMonthi onchange=javascript:ResetSearch();>";
            for (int iIndex = 0; iIndex < dtMonthi.Rows.Count; iIndex++)
            {
                string MonthiId = (string)dtMonthi.Rows[iIndex]["MonthiId"];
                string Name = (string)dtMonthi.Rows[iIndex]["Name"];
                SelectMonthiText += "<option value=\"" + MonthiId + "\">" + Name + "</option>";
            }
            SelectMonthiText += "</select>";
            return SelectMonthiText;
        }
    }
}